<?php
namespace app\models;

use yii;
use yii\db\Query;

class Purchase_order
{
    private $table;

    public function __construct()
    {
        $this->table = yii::$app->db->tablePrefix.'purchase_order';
    }

    /**更新采购订单状态
     * 
     * @param $order 采购订单
     * @return bool
     */
    public function update_status($order, $status)
    {
        if ($this->select_order($order)) {
            //查询是否存在记录
            return yii::$app->db->createCommand()
            ->update($this->table, ['status'=>$status], ['purchase_order'=>$order])
            ->execute();
        } else {
            return yii::$app->db->createCommand()
            ->insert($this->table, ['purchase_order'=>$order,'status'=>$status])
            ->execute();
        }
    }

    /**查询采购订单
     * 
     * @param $order 采购订单号
     */
    public function select_order($order)
    {
        $db = new Query();
        return $db->select('id')->from($this->table)->where(['purchase_order'=>$order])->one();
    }

    /**
     * 通过采购列表查询采购列表
     * @param $order array 采购列表
     * @return array
     */
    public function get_list_by_purchase_order($order)
    {
        if (count($order)<=0) {
            return [];
        }
        $db = new Query();
        $list = $db->select('*')
        ->from($this->table)
        ->where(['in','purchase_order',$order])
        ->all();
        $arr = [];
        foreach ($list as $v) {
            $arr[$v['purchase_order']] = $v;
        };
        unset($list);
        return $arr;
    }
}
